/*
 * 
 *
 * Authors: Soeren Gebbert, soeren.gebbert@vti.bund.de
 *          Axel Don, axel.don@vti.bund.de
 *
 * Copyright:
 *
 * Johann Heinrich von Thünen-Institut
 * Institut für Agrarrelevante Klimaforschung
 *
 * Phone: +49 (0)531 596 2601
 *
 * Fax:+49 (0)531 596 2699
 *
 * Mail: ak@vti.bund.de
 *
 * Bundesallee 50
 * 38116 Braunschweig
 * Germany
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <iostream>
#include <sstream>
#include <fstream>
#include "runtime_parameters.h"
using namespace std;

/* -------------------------------------------------------------------------- */
runtime_parameters::runtime_parameters() {
    this->timestep = 0;
    this->dxmicr = 1.0;
    this->dymicr = 1.0;
    this->dzmicr = 1.0;
    this->dxcarb = 1.0;
    this->dycarb = 1.0;
    this->dzcarb = 1.0;
    this->carbon_flow = 0.0;
    this->new_carb_particles = 250;
    this->total_carb_input_mass = 33.3333333333333;
    this->respiration_rate = 0.03;
    this->radius = 0.5;
    this->doc_fraction = 0;
}
/* -------------------------------------------------------------------------- */
void runtime_parameters::read(char *filename) {

    char buff[1024];
    
    ifstream input(filename, ifstream::in);
        
    if(input) {
        while (input.getline(buff, 1024)) {

            string line(buff);
            string sub;
            unsigned int pos;
            /* Ignore lines with a leading #, its a comment */
            if (line.find("#") != string::npos)
                continue;

            if (line.find("timestep") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->timestep;
                }
            }
            if (line.find("dxmicr") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->dxmicr;
                }
            }
            if (line.find("dymicr") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->dymicr;
                }
            }
            if (line.find("dzmicr") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->dzmicr;
                }
            }
            if (line.find("dxcarb") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->dxcarb;
                }
            }
            if (line.find("dycarb") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->dycarb;
                }
            }
            if (line.find("dzcarb") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->dzcarb;
                }
            }
            if (line.find("new_carb_particles") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->new_carb_particles;
                }
            }
            if (line.find("total_carb_input_mass") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->total_carb_input_mass;
                }
            }
            if (line.find("respiration_rate") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->respiration_rate;
                }
            }
            if (line.find("radius") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->radius;
                }
            }
            if (line.find("carbon_flow") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->carbon_flow;
                }
            }
            if (line.find("doc_fraction") != string::npos) {
                pos = line.find("=");
                if (pos != string::npos) {
                    sub = line.substr(pos + 1);
                    stringstream ss(sub);
                    ss >> this->doc_fraction;
                }
            }
        }
    } else {
        cerr << "Unable to open or read initial parameter file " << filename << endl;
    }

    input.close();
}
